package com.gwsdk.paysdk.acitvityV2.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwsdk.paysdk.bean.CouponBean;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.dialog.GWTipDialog;
import com.gwsdk.paysdk.http.process.CouponReceiveProcess;
import com.gwsdk.paysdk.listener.OnMultiClickListener;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.GWLog;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class GWCouponAdapter extends BaseAdapter {

    private static final String TAG = "GWCouponAdapter";

    private Activity activity;
    private LayoutInflater mInflater;
    private List<CouponBean> list = new ArrayList<CouponBean>();
    private int selectPosition = -1;
    private Handler couponListHandler;

    private int type;  // 1可领取  2已领取 3已失效


    private Boolean isSelected = false;

    public void setType(int type) {
        this.type = type;
    }

    public GWCouponAdapter(Activity act, Handler handler,Boolean isSelected){
        this.activity = act;
        this.isSelected = isSelected;
        couponListHandler = handler;
        mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<CouponBean> listData){
        this.list = listData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
       return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    class ViewHolder {
        private TextView tvPrice,tvTitle,btnOk,tvNum,tvName;
        private LinearLayout layoutBg;
        private GWTipDialog tipDialog;
        private ImageView imgChoose;
        public void receiveCoupon(CouponBean couponModel){
            couponModel.setStatusType(2);
            tipDialog = new GWTipDialog.Builder().setMessage("正在领取...").show(activity, activity.getFragmentManager());
            CouponReceiveProcess process = new CouponReceiveProcess();
            process.setCoupon_id(couponModel.getId());
            process.post(handler);
        }

        Handler handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                tipDialog.dismissAllowingStateLoss();
                switch (msg.what){
                    case Constant.RECEIVE_COUPON_SUCCESS:
                        notifyDataSetChanged();
                        GWLog.w(TAG, "#handleMessage_领取Coupon");
                        break;
                    case Constant.RECEIVE_COUPON_FAIL:
                        if (!TextUtils.isEmpty(msg.obj.toString())){
                            ToastUtil.show(activity,msg.obj.toString());
                        }
                        break;
                }
            }
        };
    }


    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final CouponBean couponModel = list.get(i);
        final ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(GWInflaterUtils.getLayout(activity,"gw_item_coupon"), null);

            viewHolder.layoutBg = convertView.findViewById(GWInflaterUtils.getControl(activity,"layout_bg"));
            viewHolder.tvPrice = convertView.findViewById(GWInflaterUtils.getControl(activity,"tv_coupon_price"));
            viewHolder.tvTitle = convertView.findViewById(GWInflaterUtils.getControl(activity,"tv_title"));
            viewHolder.btnOk = convertView.findViewById(GWInflaterUtils.getControl(activity,"btn_ok"));
            viewHolder.tvNum = convertView.findViewById(GWInflaterUtils.getControl(activity,"tv_num"));
            viewHolder.tvName = convertView.findViewById(GWInflaterUtils.getControl(activity,"tv_name"));
            viewHolder.imgChoose = convertView.findViewById(GWInflaterUtils.getControl(activity,"img_choose"));
            viewHolder.tvNum.setVisibility(View.GONE);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvName.setText(couponModel.getCoupon_name());

        if (couponModel.getStatusType() == 1){  //可领取
//            viewHolder.layoutBg.setBackgroundResource(GWInflaterUtils.getIdByName(activity, "drawable", "mch_coupon_bg_n"));
            viewHolder.tvPrice.setText(couponModel.getMoney());
//            viewHolder.tvTime.setText("有效期"+couponModel.getStart_time() + "至" + couponModel.getEnd_time());
            if (couponModel.getLimit_money().equals("0")){
                viewHolder.tvTitle.setText("无门槛");
            }else {
                viewHolder.tvTitle.setText("满" + couponModel.getLimit_money() + "元可用");
            }
            viewHolder.btnOk.setBackgroundResource(GWInflaterUtils.getIdByName(activity, "drawable", "gw_coupon_button_linght"));
            viewHolder.btnOk.setEnabled(true);
            viewHolder.btnOk.setTextColor(Color.parseColor("#DE161E"));
            viewHolder.btnOk.setText("领取");
            viewHolder.btnOk.setOnClickListener(new OnMultiClickListener() {
                @Override
                public void onMultiClick(View view) {
                    viewHolder.receiveCoupon(couponModel);
                }
            });

            if (couponModel.getLimit()==0){
                viewHolder.tvNum.setVisibility(View.INVISIBLE);
            }else {
                viewHolder.tvNum.setVisibility(View.VISIBLE);
                viewHolder.tvNum.setText("剩余" + couponModel.getLimit_num() + "张");
            }
        }

        if (couponModel.getStatusType() == 2){   //已领取
//            viewHolder.layoutBg.setBackgroundResource(GWInflaterUtils.getIdByName(activity, "drawable", "mch_coupon_bg_n"));
            viewHolder.tvPrice.setText(couponModel.getMoney());
//            viewHolder.tvTime.setText("有效期"+couponModel.getStart_time() + "至" + couponModel.getEnd_time());
            if (couponModel.getLimit_money().equals("0")){
                viewHolder.tvTitle.setText("无门槛");
            }else {
                viewHolder.tvTitle.setText("满" + couponModel.getLimit_money() + "元可用");
            }
            viewHolder.btnOk.setEnabled(false);
            viewHolder.btnOk.setTextColor(Color.parseColor("#9F9F9F"));
            viewHolder.btnOk.setBackgroundResource(GWInflaterUtils.getDrawable(activity,"gw_20dp_coupon_bg_transparent"));
            viewHolder.btnOk.setText("已领");
            viewHolder.tvNum.setVisibility(View.INVISIBLE);

            if (isSelected){
                viewHolder.btnOk.setVisibility(View.GONE);
            }
            viewHolder.tvNum.setVisibility(View.VISIBLE);
            if ("永久".equals(couponModel.getEnd_time())){
                viewHolder.tvNum.setText("永久有效");
            }else {
                viewHolder.tvNum.setText("有效期至 " + couponModel.getEnd_time());
            }

        }

        if (couponModel.getStatusType() == 3){  //已使用
//            viewHolder.layoutBg.setBackgroundResource(GWInflaterUtils.getIdByName(activity, "drawable", "mch_coupon_bg_d"));
            viewHolder.tvPrice.setText(couponModel.getMoney());
//            viewHolder.tvTime.setText("有效期"+couponModel.getStart_time() + "至" + couponModel.getEnd_time());
            if (couponModel.getLimit_money().equals("0")){
                viewHolder.tvTitle.setText("无门槛");
            }else {
                viewHolder.tvTitle.setText("满" + couponModel.getLimit_money() + "元可用");
            }
            viewHolder.btnOk.setEnabled(false);
            viewHolder.btnOk.setTextColor(Color.parseColor("#ffffff"));
            viewHolder.btnOk.setBackgroundResource(GWInflaterUtils.getDrawable(activity,"gw_30dp_bg_hui"));
            viewHolder.btnOk.setText("已使用");
            viewHolder.tvNum.setVisibility(View.GONE);
        }

        if (couponModel.getStatusType() == 4){  //已过期
//            viewHolder.layoutBg.setBackgroundResource(GWInflaterUtils.getIdByName(activity, "drawable", "mch_coupon_bg_d"));
            viewHolder.tvPrice.setText(couponModel.getMoney());
//            viewHolder.tvTime.setText("有效期"+couponModel.getStart_time() + "至" + couponModel.getEnd_time());
            if (couponModel.getLimit_money().equals("0")){
                viewHolder.tvTitle.setText("无门槛");
            }else {
                viewHolder.tvTitle.setText("满" + couponModel.getLimit_money() + "元可用");
            }
            viewHolder.btnOk.setEnabled(false);
            viewHolder.btnOk.setTextColor(Color.parseColor("#ffffff"));
            viewHolder.btnOk.setBackgroundResource(GWInflaterUtils.getDrawable(activity,"gw_30dp_bg_hui"));
            viewHolder.btnOk.setText("已过期");
            viewHolder.tvNum.setVisibility(View.GONE);
        }


        viewHolder.imgChoose.setVisibility(View.GONE);
        if (isSelected){
            if(couponModel.isSelected()){
                viewHolder.imgChoose.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

}
